/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Khachhang;
import pojo.Phieuxuathang;
import util.HibernateUtil;

/**
 *
 * @author dell pro
 */
public class KhachHangDAO {
     public static List<Khachhang> layDanhSachKhachhang() {
        List<Khachhang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kh from Khachhang kh";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
     public static boolean themKhachHang(Khachhang khachHang){
       Session session = HibernateUtil.getSessionFactory().openSession();
       boolean kq = true;
       Transaction transaction = null;
       try {
          transaction = session.beginTransaction();
          session.saveOrUpdate(khachHang); 
          transaction.commit();
       } catch (Exception e) {
           transaction.rollback(); 
           System.err.println(e); 
           kq = false;
       } finally { session.close(); }
       return kq;
   }
   
    public static Khachhang GetKhachhang(String id){
        Khachhang khachHang = new Khachhang();
        List<Khachhang> listkh = KhachHangDAO.layDanhSachKhachhang();
        for (int i=0;i<listkh.size();i++){
            if (id.equals(listkh.get(i).getIdkhachhang())){
                khachHang = listkh.get(i);
            }
        }
        return khachHang;
    }
    
    public static boolean Exist(String id){
       boolean check = false;
       List<Khachhang> listkh = KhachHangDAO.layDanhSachKhachhang();
         for (int i=0;i<listkh.size();i++){
             if(id.equals(listkh.get(i).getIdkhachhang())){
                 check = true;
                 break;
             }
         }
       return check;
    }
    
    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Khachhang> listpnh = KhachHangDAO.layDanhSachKhachhang();
        if (KhachHangDAO.layDanhSachKhachhang().size() == 0) {
                chuoi += "mkh001";
                return chuoi;
        }
        macuoicung += listpnh.get(KhachHangDAO.layDanhSachKhachhang().size() - 1).getIdkhachhang();
        String index = macuoicung.substring(3);
        try {
            
                //System.out.println("Index la"+index+"");
                int chiso = Integer.valueOf(index);
                chiso++;

                if (chiso < 10) {
                    chuoi += "mkh00" + chiso;
                } else if (chiso >= 10 && chiso < 100) {
                    chuoi += "mkh0" + chiso;
                } else if (chiso >= 1000) {
                    chuoi += "mkh" + chiso;
                }
            
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    
    
}
